package com.example.cofeed.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.example.cofeed.MainActivity;
import com.example.cofeed.R;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.constant.SputilsConstant;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

/**
 * Created by mqwang on 2017/12/11.
 */

public class GuideActivity extends BaseActivity {

    private ArrayList<View> layouts = new ArrayList<>();
    private ViewPager viewPager;
    private int[] mGuideImageId=new int[]{
            R.mipmap.guide_one,
            R.mipmap.guide_two,
            R.mipmap.guide_three,
    };

    @Override
    protected boolean isNeedTitleBar() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_guide;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        SPUtils.getInstance().put(SputilsConstant.IS_FIRST, false);
        View layout1 =getLayoutInflater().inflate(R.layout.guide_item, null);
        View layout2 =getLayoutInflater().inflate(R.layout.guide_item, null);
        View layout3 =getLayoutInflater().inflate(R.layout.guide_item, null);
        layouts.add(layout1);
        layouts.add(layout2);
        layouts.add(layout3);
        findViewById(R.id.btn_enter).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMain();            }
        });
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setAdapter(new PagerAdapter(){
            @Override
            public Object instantiateItem(View container, int position)
            {
                View layout = layouts.get(position);
                layout.setBackgroundResource(mGuideImageId[position]);
                viewPager.addView(layout);
                return layout;
            }

            @Override
            public void destroyItem(View container, int position, Object object){
                View layout = layouts.get(position);
                viewPager.removeView(layout);
            }

            @Override
            public boolean isViewFromObject(View view, Object object)
            {
                return view==object;
            }

            @Override
            public int getCount()
            {
                return layouts.size();
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 2){
                    findViewById(R.id.btn_enter).setVisibility(View.VISIBLE);
                }else {
                    findViewById(R.id.btn_enter).setVisibility(View.GONE);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    void goMain(){
//        String token = SPUtils.getInstance().getString(SputilsConstant.TOKEN);
//        if (!TextUtils.isEmpty(token)){
//            ActivityUtils.startActivity(MainActivity.class);
//        }else if (SPUtils.getInstance().getBoolean(SputilsConstant.IS_FIRST, true)){
//            ActivityUtils.startActivity(GuideActivity.class);
//        }else {
//            ActivityUtils.startActivity(LoginActivity.class);
//        }
        ActivityUtils.startActivity(MainActivity.class);

        finish();
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
